﻿using Pairingproject2._3.answer;
using Pairingproject2._3.Computea;
using Pairingproject2._3.DefinitionS;
using Pairingproject2._3.GenerateTheTitle;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Xml;

namespace Pairingproject2._3.UI
{
    public partial class UIClass//UI测试界面//尚未实现文件流方式

    {
        public static string username;//用户名称
        Generatethetitle n_puzzle = new Generatethetitle();//生成题目类N_Puzzle
        Compute compute = new Compute();//计算类
        public string[] answers = new string[1000];//答案保存，最大1000//
        public static int num = 0;//记录输入题数
        public static char type = 'A';//默认年级为A
        AnswerClass Puzzle;
        Definition definition = new Definition();//插入括号类

        #region 将题目记录下来生成文件
        public static string[] RecordTheTitle;//将题目保存
        public static string[] RecordTheAnswer;//保存用户输入的答案
        #endregion

        //--------------开始答题-----------
        public void Startingtowork()
        {
            while (!verification())
            {
                verification();
            }
            //条件测试中，默认为真

            //---------生成num道题目------------------ -      
            string[] topics = n_puzzle.PuzzleGenerate(num, type);
            RecordTheTitle = topics;
            if (type == 'C')
            {
                for (int i = 0; i < topics.Length; i++)
                {
                    topics[i] = definition.InsertTheBrackets(topics[i]);
                }
            }
            Puzzle = new AnswerClass();
            Puzzle.userAnswer = "0";
            for (int i = 0; i < topics.Length; i++)//可合并
            {
                Puzzle.result[i] = compute.Calculate(topics[i]);
            }
            Console.WriteLine("即将进入答题，输入esc即可退出答题");
            RecordTheAnswer = new string[topics.Length];
            for (int i = 0; i < topics.Length; i++)
            {
                Console.WriteLine("-------------------------------");
                Console.WriteLine("第{0}题", i + 1);
                Console.WriteLine(topics[i]);
                Puzzle.userAnswer = Console.ReadLine();
                if (i == 0)
                {
                    Puzzle.userAnswer = Console.ReadLine();
                }
                if (Puzzle.userAnswer == "esc")
                {
                    break;
                }
                Puzzle.CheckAnswer(i);
                RecordTheAnswer[i] = Puzzle.userAnswer;
                Console.WriteLine("-------------------------------");
            }
            Console.WriteLine("答题结束");
            Console.WriteLine("是否生成错题本,确认请按1，取消按2");
            int WrongTopicThis = Convert.ToInt32(Console.ReadLine());
            if (WrongTopicThis == 1)
            {
                CreateSampleFileXMLAsync("Text.xml");
                Console.WriteLine("生成错题本成功");
            }
            Console.WriteLine("祝您生活愉快");

        }
        public bool verification()
        {
            bool assist = true;
            Console.WriteLine("请输入Name");
            username = Console.ReadLine();
            //-------------检查输入是否合法-------------------        
            if (username == "")
            {
                Console.WriteLine("请输入您的用户名");
                assist = false;
                //Startingtowork();//死循环
            }
            else if (username != "张三")
            {
                Console.WriteLine("用户名输入错误");
                assist = false;
            }
            Console.WriteLine("输入出题数");
            num = Convert.ToInt32(Console.ReadLine());//未添加异常处理
            if (num <= 0)
            {
                Console.WriteLine("题数必须是大于0");
                assist = false;
            }

            Console.WriteLine("输入年级");
            Console.WriteLine("A代表一年级，B代表二年级，以此类推");
            type = Convert.ToChar(Console.Read());//未添加异常处理
            //if (type==null)
            //{
            //    Console.WriteLine("年级不可为空");
            //    assist = false;
            //}
            return assist;
        }


        //------------------查看历史记录-----------//未实现
        //private void History()
        //{
        //    string s = "张三";
        //    if (s == "")//获取用户输入名称
        //    {
        //        Console.WriteLine("请输入你的ID");
        //    }
        //    else
        //    {
        //        username = "张三";
        //        History Record = new History();
        //    }
        //}




        #region 生成文件
        private static void CreateSampleFileXMLAsync(string fileName)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            using (XmlWriter writer = XmlWriter.Create("..\\..\\..\\" + fileName, settings))
            {
                int n = 0;
                writer.WriteStartElement("题目");
                foreach (var i in RecordTheTitle)
                {
                    writer.WriteElementString("Answer", $"{i}");
                    writer.WriteElementString("Mistake", $"{RecordTheAnswer[n++]}");
                }
                writer.WriteEndElement();
            }
        }

        #endregion
    }
}
